DeleteLeaveRequestCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 22
dl 0
loc 25
c 0
b 0
f 0
rs 10

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 17 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { DeleteLeaveRequestCommand } from './DeleteLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
6
import { DoesLeaveRequestBelongToUser } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestBelongToUser';
7
import { LeaveRequestCantBeRemovedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeRemovedException';
8
9
@CommandHandler(DeleteLeaveRequestCommand)
10
export class DeleteLeaveRequestCommandHandler {
11
  constructor(
12
    @Inject('ILeaveRequestRepository')
13
    private readonly leaveRequestRepository: ILeaveRequestRepository,
14
    private readonly doesLeaveRequestBelongToUser: DoesLeaveRequestBelongToUser
15
  ) {}
16
17
  public async execute(command: DeleteLeaveRequestCommand): Promise<void> {
18
    const { owner, id } = command;
19
20
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
21
    if (!leaveRequest) {
22
      throw new LeaveRequestNotFoundException();
23
    }
24
25
    if (
26
      false ===
27
      this.doesLeaveRequestBelongToUser.isSatisfiedBy(leaveRequest, owner)
28
    ) {
29
      throw new LeaveRequestCantBeRemovedException();
30
    }
31
32
    this.leaveRequestRepository.remove(leaveRequest);
33
  }
34
}
35